--
-- Author: shunguo.chen
-- Date: 2019-05-16 16:02:00
--
-- LegendController 传奇武器ctrl
--

local LegendView = import("..views.LegendView")
local LegendPreView = import("..views.LegendPreView")
local LegendNewView = import("..views.LegendNewView")
local LegendResetView = import("..views.LegendResetView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("LegendModel")

    self._curIdx = 1 -- 面板idx
    self._selectStarId = nil -- 选中的星魂

    self._skillHandler = UD:onChanged("skill", function()
        self._model:clearSkillCache()
    end)
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)

    UD:unChanged("skill", self._skillHandler)
end

function ClassRef:listEvent()
    return { }
end

-- data = funcId
function ClassRef:openView(data)
    self._model:C2S_GetLegendInfo(function(legendInfo)
        if nil == self._view then
            self._view = LegendView.new(app.mvc.VIEWTYPE_BLUR, legendInfo)
            self._view:onCloseView(handler(self, self.closeView))
            self._view:onClearView(handler(self, self.clearView))
            self._view:onOperate(handler(self, self.onOperate))
            self._view:onSwitchPanel(handler(self, self.onSwitchPanel))
            self:addChild(self._view, data)
        end

        local idx
        if data == Constant.FUNC_TYPE_LEGEND then
            idx = 1
        elseif data == Constant.FUNC_TYPE_LEGEND_STARMAP then
            idx = 2
        elseif data == Constant.FUNC_TYPE_LEGEND_FASHION then
            idx = 3
        end
        self._view:show(idx or 1)
        self._view:refreshRedPoint(UD.badge:getNum(Constant.FUNC_TYPE_LEGEND) > 0)
    end)
end
function ClassRef:clearView()
    if self._view then
        self._view = nil

        self._model:clearLegendNew()
    end
end

-- 打开神器获得界面
function ClassRef:openNewView(legend)
    local view = LegendNewView.new()
    view:onCloseView(function(sender)
        UIHandler:removePopupUiByKey(sender:getViewName())
    end)
    view:refresh(legend)
    UIHandler:addPopupUi(view, view:getViewName())
    view:show()
end

-- 打开外显预览界面
function ClassRef:openPreView(legend)
    local view = LegendPreView.new()
    view:onCloseView(function(sender)
        UIHandler:removePopupUiByKey(sender:getViewName())
    end)
    view:refresh(legend)
    UIHandler:addPopupUi(view, view:getViewName())
    view:show()
end

-- 打开神器重置界面
function ClassRef:openResetView(cosItem, gotItem, showTip, cb)
    if not self:isViewAlive() then return end
    local view = self._view:getSubView("LegendResetView")
    if not view then
        view = LegendResetView.new()
        view:onCloseView(function(sender)
            sender:setVisible(false)
        end)
        self._view:addSubView(view, "LegendResetView", 100)
    end
    view:onOperate(function(op, data)
        executeFunc(cb)
    end)
    view:refresh(cosItem, gotItem, showTip)
    view:show()
end

-- 装备神器弹框是否穿戴外显
function ClassRef:openLegendEquipDlg(legend)
    local isWithFashion = false
    if not legend:isWear() then
        -- 装备神器
        if legend:hasFashion() then
            local dlg = display.newConfirmDialog(L("LegendBasic0_tips1"),
                    function() -- 确定
                        self._model:C2S_EquipLegend(legend, true)
                    end,
                    function()
                        self._model:C2S_EquipLegend(legend, false)
                    end)
            UIHandler:addPopDialog( dlg )
        else
            self._model:C2S_EquipLegend(legend, false)
        end
    else
        -- 卸下神器
        local dlg = display.newConfirmDialog(L("LegendBasic0_tips2"),
                function() -- 确定
                    self._model:C2S_EquipLegend(legend, false)
                end)
        UIHandler:addPopDialog( dlg )
    end
end

-- 卸下已装备技能弹框提示
function ClassRef:openSkillChooseDlg(legend, skill)
    if skill:isLegendChoose() then
        local dlg = display.newConfirmDialog(L("LegendBasic3_tips4"),
                function() -- 确定
                    self._model:C2S_ChooseLegendSkill(legend, skill)
                end)
        UIHandler:addPopDialog( dlg )
    else
        self._model:C2S_ChooseLegendSkill(legend, skill)
    end
end

-------------------------------------------------------
-- 神器装备/卸下
function ClassRef:onLegendEquip(legend)
    if not self:isViewAlive() then return end
    if self._curIdx ~= 1 then return end

    self:onSwitchPanel(1)
end

-- 神器升级
function ClassRef:onLegendLvup(legend)
    if not self:isViewAlive() then return end
    if self._curIdx ~= 1 then return end

    self:onSwitchPanel(1)
end

-- 神器回收
function ClassRef:onLegendReset(legend)
    if not self:isViewAlive() then return end
    if self._curIdx ~= 1 then return end

    self:onSwitchPanel(1)
end

-- 神器技能激活
function ClassRef:onLegendSkillActive(legend, skill)
    if not self:isViewAlive() then return end
    if self._curIdx ~= 1 then return end
    
    local panel = self._view:getPanel(self._curIdx)
    panel:refreshSkillPanel()
    panel:refreshRedPoint()
end

-- 神器技能装备/卸下
function ClassRef:onLegendSkillChoose(legend, skill)
    if not self:isViewAlive() then return end
    if self._curIdx ~= 1 then return end
    
    local panel = self._view:getPanel(self._curIdx)
    panel:refreshSkillPanel()
end

-- 神器技能回收
function ClassRef:onLegendSkillReset(legend)
    if not self:isViewAlive() then return end
    if self._curIdx ~= 1 then return end
    
    local panel = self._view:getPanel(self._curIdx)
    panel:refreshSkillPanel()
    panel:refreshRedPoint()
end

-- 神器星图更新 激活/升级
function ClassRef:onLegendStarUpdate(starMap, star)
    if not self:isViewAlive() then return end
    if self._curIdx ~= 2 then return end

    self._selectStarId = star:getId()
    self:onSwitchPanel(2)
end

-- 神器外显熔炼
function ClassRef:onLegendFashionActive(legend, fashion)
    if not self:isViewAlive() then return end
    if self._curIdx ~= 3 then return end
    
    self:onSwitchPanel(3)
end

-------------------------------------------------------
function ClassRef:onOperate(op, ...)
    local data = ...
    print("LegendController:onOperate op", op)
    if op == "legend_equip" then
        -- 传奇武器装备 ... = LegendVo
        self:openLegendEquipDlg(...)
    elseif op == "legend_get" then
        -- 获得传奇武器 ... = LegendVo
        local param = data:getResourceParam()
        if param then
            display.resourceGotoFeature(param)
        else
            display.pushToast(L("sourceTips1"))
        end
    elseif op == "legend_preview" then
        -- 传奇武器外形预览 ... = LegendVo
        self:openPreView(...)
    elseif op == "legend_reset" then
        -- 传奇武器重置 ... = LegendVo
        local costItem = data:getResetExpCostItem()
        local gotItem = data:getResetExpGotItem()
        self:openResetView(costItem, gotItem, true, function()
            if data:isNeedReset() then
                self._model:C2S_RecycleLegendLevel(data)
            else
                display.pushToast(L("ErrLegendCanNotRecycle"))
            end
        end)
    elseif op == "legend_lvup" then
        -- 传奇武器升级 ...= LegendVo, num
        self._model:C2S_LevelUpLegend(...)
    elseif op == "legend_resetSkill" then
        -- 传奇武器技能点重置 ... = LegendVo
        local costItem = data:getResetSkillCostItem()
        local gotItem = data:getResetSkillGotItem()
        self:openResetView(costItem, gotItem, false, function()
            if data:hasOpenSkill() then
                self._model:C2S_RecycleLegendSkills(data)
            else
                display.pushToast(L("LegendBasic3_tips3"))
            end
        end)
    elseif op == "legend_gotoSkill" then
        -- 传奇武器 打开技能界面配置配置技能
        UIHandler:handleMessage({id=Constant.FUNC_TYPE_SKILL, mode="open"})
    elseif op == "legend_selectSkill" then
        -- 传奇武器 选择/不选择额外技能 ... = LegendVo, Skill
        self._model:C2S_ChooseLegendSkill(...)
    elseif op == "legend_openSkill" then
        -- 传奇武器 打开额外技能 ... = LegendVo, Skill
        self._model:C2S_ActiveLegendSkill(...)
    elseif op == "legend_activeFashion" then
        -- 传奇武器 激活时装 ... = LegendVo, LegendFashion
        self._model:C2S_MeltingLegend(...)
    elseif op == "star_lvup" then
        -- 传奇星图升级 ... = LegendStarMap, LegendStar
        self._model:C2S_ActiveLegendStarMapSkill(...)
    end
end

-- 1 LegendPanel
-- 2 LegendStarPanel
-- 3 LegendFashionPanel
function ClassRef:onSwitchPanel(idx)
    self._curIdx = idx
    if idx == 1 then
        self:refreshPanel()
    elseif idx == 2 then
        self:refreshStarPanel()
    elseif idx == 3 then
        self:refreshFashionPanel()
    end
end

function ClassRef:refreshPanel()
    if self._curIdx ~= 1 then return end

    local info = self._model:getInfo()
    local panel = self._view:getPanel(1)
    panel:refreshList(info, self._curIdx)
end
function ClassRef:refreshStarPanel()
    if self._curIdx ~= 2 then return end

    local info = self._model:getInfo()
    local panel = self._view:getPanel(2)
    panel:refresh(info, self._selectStarId)
    self._selectStarId = nil
end
function ClassRef:refreshFashionPanel()
    if self._curIdx ~= 3 then return end

    local info = self._model:getInfo()
    local panel = self._view:getPanel(3)
    panel:refreshList(info, self._curIdx)
end

return ClassRef
